package com.dongdongshop.operator.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.pojo.TbSpecification;
import com.dongdongshop.operator.service.SpecificationService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    private final Logger logger = LoggerFactory.getLogger(SpecificationController.class);

    @Reference(check = false)
    private SpecificationService specificationService;

    //跳转到规格管理页面
    @RequestMapping("toSpecification")
    public String toSpecification(){
        return "admin/specification";
    }

    //获取规格管理列表
    @ResponseBody
    @RequestMapping("getSpecification")
    public Result getSpecification(String specName){
        try {
            List<TbSpecification> specificationList = specificationService.getSpecification(specName);
            return Result.success(ResultEnum.SUCCESS,specificationList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e)                     ;
            return Result.response(ResultEnum.FAIT);
        }
    }

    //根据id查询主表和次表----回显
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            List<TbSpecification> list = specificationService.getByid(id);
            return Result.success(ResultEnum.SUCCESS,list);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //修改操作,修改主表和次表
    @RequestMapping("updateSpecification")
    @ResponseBody
    public Result updateSpecification(TbSpecification specification, @RequestParam(value = "optionNameList[]")String[] optionNameList,
                                      @RequestParam(value = "orderList[]")Integer[] orderList){
        try {
            specificationService.updateSpecification(specification,optionNameList,orderList);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //批量删除
    @RequestMapping("deleteSpecificationByIds")
    @ResponseBody
    public Result deleteSpecificationByIds(@RequestParam("ids[]")Long[] ids){
        try {
            specificationService.deleteSpecificationByIds(ids);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //增加
    @ResponseBody
    @RequestMapping("addSpecification")
    public Result addSpecification(TbSpecification specification, @RequestParam(value = "optionNameList[]")String[] optionNameList,
                                   @RequestParam(value = "orderList[]")Integer[] orderList){
        try {
            specificationService.addSpecification(specification,optionNameList,orderList);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
}
